package com.community.service.impl;

import com.community.annotation.SysLog;
import com.community.base.BaseService;
import com.community.common.Const;
import com.community.common.ServerResponse;
import com.community.pojo.AnswerComment;
import com.community.pojo.CommentOperation;
import com.community.service.ICommentOperationService;
import com.community.util.LoginUtil;
import com.community.util.NumberUtil;
import com.community.util.StringUtil;
import com.community.util.ToolUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Map;

/**
 * 评论操作服务层
 */
@Service("iCommentOperationService")
public class CommentOperationServiceImpl extends BaseService implements ICommentOperationService {

    /**
     * 评论操作
     * @param paramMap
     * @param request
     * @return
     */
    @SysLog("评论操作")
    public ServerResponse<String> commentOperation(Map paramMap, HttpServletRequest request){
        String userName = StringUtil.valueOfString(paramMap.get("userName"));
        if(!LoginUtil.isLogin(request,userName)){
            return ServerResponse.createByErrorMessage(Const.Msg.PARAMTERERROR);
        }
        int cid = NumberUtil.safeToNumber(paramMap.get("cid"));
        int type = NumberUtil.safeToNumber(paramMap.get("type"));
        //查询用户是否已经进行对应操作(生效状态的)
        int coutNum = commentOperationMapper.findByParam(cid,type,userName);
        if(coutNum > 0){
            //如果是赞或踩
            if(type == Const.AnswerOpertion.AGREE || type == Const.AnswerOpertion.OPPOSE ){
                //如果类型是赞同或者反对，则删除其赞同或者反对
                commentOperationMapper.delByParam(cid,type,userName);
                if(type == Const.AnswerOpertion.AGREE){
                    //更新评论点赞数
                    AnswerComment answerComment = answerCommentMapper.selectByPrimaryKey(cid);
                    if(NumberUtil.safeToNumber(answerComment.getLikeNums()) > 0){
                        int likeNums = answerComment.getLikeNums() - 1;
                        answerComment.setLikeNums(likeNums);
                        answerCommentMapper.updateByPrimaryKeySelective(answerComment);
                    }
                }
                return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
            }
        }
        CommentOperation commentOperation = new CommentOperation();
        commentOperation.setCid(NumberUtil.safeToNumber(paramMap.get("cid")));//评论ID
        commentOperation.setType(NumberUtil.safeToNumber(paramMap.get("type")));//操作类型
        commentOperation.setReporttype(NumberUtil.safeToNumber(paramMap.get("reportType")));//举报类型
        commentOperation.setContent(StringUtil.valueOfString(paramMap.get("content")));//举报内容
        commentOperation.setTimes(new Date());//操作时间
        commentOperation.setIp(ToolUtil.getClientIp(request));//ip
        commentOperation.setUsername(userName);//用户名
        if(commentOperationMapper.insert(commentOperation) > 0){
            //更新评论点赞数
            answerCommentMapper.plusAnswerCommentLikeNums(cid);
            return ServerResponse.createBySuccessMessage(Const.Msg.SAVESUCCESS);
        }
        return ServerResponse.createByErrorMessage(Const.Msg.SAVEERROR);
    }

}
